﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using xTeam.HengLe.Entity;
using Valor.Helper;
using xTeam.HengLe.DataAccess;
using System.Web.Script.Serialization;

namespace xTeam.HengLe.Controller.Website
{
    public class HomeController : BaseController
    {
        private AdvertiseDataAccess _advertiseDataAccess;

        public HomeController()
        {
            _advertiseDataAccess = new AdvertiseDataAccess(_connectionString);
        }
        public ActionResult Index()
        {
            HomeViewModel viewModel = new HomeViewModel() { ImageRoot = _imageRoot };
          

            int pageCount = 0;
           // List<EProduct> productlist = _productDataAccess.Search(3, 0, 0, string.Empty, ref pageCount);
            List<ESlide> commendList = null;
            try
            { 
                EAdvertise e = _advertiseDataAccess.SelectById(1);
                JavaScriptSerializer json = new JavaScriptSerializer();
                commendList =json.Deserialize<List<ESlide>>(e.Contents)?? new List<ESlide>();
            }
            catch
            {
              commendList =   new List<ESlide>();
            }

            viewModel.CommandList = commendList;
            return View(viewModel);
        }

        public ActionResult Thumb(int w = 300, int h = 100, string s = "")
        {
            w = w <= 1 ? 300 : w;
            h = h <= 1 ? 100 : h;
            if (System.IO.File.Exists(Server.MapPath(s)))
            {
                Response.ContentType = "images/jpeg";
                System.IO.MemoryStream ms = ThumbImageHelper.CuteThumbPhoto(w, h, Server.MapPath(s), false, true);
                Response.BinaryWrite(ms.ToArray());

            }
            return new EmptyResult();
        }
    }
}
